package OSdesign.Function_show;


import javax.management.relation.RelationNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MatchManager {
    public MatchManager(){
        //构造函数
    }
    public Object[][] CompetitionSet(){
        int i = 0,count = 0;
        String[] ss = new String[4];
//        Object[][] objects= new Object[30][4];
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        String sql = "select * from competition";
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                    count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects= new Object[count][4];
        System.out.println(count);
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try {
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = mysql.set.getString("mno");
                objects[i][1] = mysql.set.getString("mname");
                objects[i][2] = mysql.set.getString("mrule");
                objects[i][3] = mysql.set.getString("mrecord");
                ss[0] = mysql.set.getString("mno");   ss[1]= mysql.set.getString("mname");
                ss[2] = mysql.set.getString("mrule"); ss[3] = mysql.set.getString("mrecord");
                i++;
            }
            System.out.println("逐条执行完毕");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

    public boolean addCompetition(String[] strs){
        boolean done = false;
        //连接数据库
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        System.out.println("正在执行SQL语句");
        String sql = "insert into competition values ('"+strs[0]+"','"+strs[1]+"','"+strs[2]+"',"+Integer.valueOf(strs[3])+")";
        try {
            mysql.stmt.execute(sql);
            done=true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  done;
    }

    public boolean ediRule(String[] data){
        boolean done = false;
        //连接数据库
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        System.out.println("正在执行SQL语句");
        String sql = "update competition set mrule='"+data[1]+"' where mno='"+data[0]+"'";
        try {
            mysql.stmt.execute(sql);
            done=true;
            System.out.println(done);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  done;
    }

    public boolean applyMatch(String[] strs){
        boolean done =false;
        //连接数据库
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        System.out.println("正在执行SQL语句");
        String sql = "insert into player values('"+strs[0]+"','"+strs[1]+"','"+strs[2]+"','"+strs[3]+"','"+strs[4]+"')";
        try {
            mysql.stmt.execute(sql);
            done=true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return done;
    }

    public Object[][] autoAllot_100(){
        int i = 0,count = 0;
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        String sql = "select pno,pname from player where mname = '男子100米短跑'";
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects= new Object[count][4];
        System.out.println(count);
        //设置组号数组和道号数组进行随机分配
        int index = 0;
        int[] group = new int[count];
        int[] track = new int[count];
        for(int j = 0;j<count;j++){
            if(j%8 == 0){
                index++;
            }
            group[j]=index;
        }
        Random random = new Random();
        List<Object> list = new ArrayList<>();
        for(int j=0;j<count;j++){
            if(j%8 == 0){
                list.clear();
            }
            while(true){
                int num = random.nextInt(8)+1;
                if(!list.contains(num)){
                    list.add(num);
                    track[j]=(int)list.get(j%8);
                    break;
                }
            }
        }
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = mysql.set.getString("pno");
                objects[i][1] = mysql.set.getString("pname");
                objects[i][2] = group[i]+"";
                objects[i][3] = track[i]+"";
                i++;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

    public Object[][] autoAllot_1500(){
        int i = 0,count = 0;
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        String sql = "select pno,pname from player where mname = '男子1500米长跑'";
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects= new Object[count][4];
        System.out.println(count);
        //设置组号数组和道号数组进行随机分配
        int index = 0;
        int[] group = new int[count];
        int[] track = new int[count];
        //分组
        for(int j = 0;j<count;j++){
            if(j%8 == 0){
                index++;
            }
            group[j]=index;
        }
        //分道
        Random random = new Random();
        List<Object> list = new ArrayList<>();
        for(int j=0;j<count;j++){
            if(j%8 == 0){
                list.clear();
            }
            while(true){
                int num = random.nextInt(8)+1;
                if(!list.contains(num)){
                    list.add(num);
                    track[j]=(int)list.get(j%8);
                    break;
                }
            }
        }
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = mysql.set.getString("pno");
                objects[i][1] = mysql.set.getString("pname");
                objects[i][2] = group[i]+"";
                objects[i][3] = track[i]+"";
                i++;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

    public Object[][] autoAllot_400(){
        int i = 0,count = 0;
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        String sql = "select pno,pname from player where mname = '女子400米短跑'";
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects= new Object[count][4];
        System.out.println(count);
        //设置组号数组和道号数组进行随机分配
        int index = 0;
        int[] group = new int[count];
        int[] track = new int[count];
        for(int j = 0;j<count;j++){
            if(j%8 == 0){
                index++;
            }
            group[j]=index;
        }
        Random random = new Random();
        List<Object> list = new ArrayList<>();
        for(int j=0;j<count;j++){
            if(j%8 == 0){
                list.clear();
            }
            while(true){
                int num = random.nextInt(8)+1;
                if(!list.contains(num)){
                    list.add(num);
                    track[j]=(int)list.get(j%8);
                    break;
                }
            }
        }
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = mysql.set.getString("pno");
                objects[i][1] = mysql.set.getString("pname");
                objects[i][2] = group[i]+"";
                objects[i][3] = track[i]+"";
                i++;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

    public Object[][] playerSet(){
        int i = 0,count = 0;
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        String sql = "select * from player";
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects= new Object[count][5];
        System.out.println(count);
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try {
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = mysql.set.getString("pno");
                objects[i][1] = mysql.set.getString("pname");
                objects[i][2] = mysql.set.getString("pgender");
                objects[i][3] = mysql.set.getString("pteam");
                objects[i][4] = mysql.set.getString("mname");
                i++;
            }
            System.out.println("逐条执行完毕");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

    public static void main(String[] args){
        new MatchManager().CompetitionSet();
    }
}
